import path from 'path'
import sharp from 'sharp'

const args = process.argv.slice(2)
const tmp_input_path = args[0]
const tmp_output_path = args[1]
const outputFormat = args[2]

console.log(
  `log from sharp program:\n \t\tfile to tile: ${tmp_input_path}\t\toutput path: ${tmp_output_path}\t\tformat: ${outputFormat}`
)

try {
  if (outputFormat === 'png') {
    await sharp(path.join(tmp_input_path), { limitInputPixels: false })
      .png({
        quality: 100,
      })
      .tile({
        size: 256,
      })
      .toFile(tmp_output_path)
  } else {
    await sharp(path.join(tmp_input_path), { limitInputPixels: false })
      .jpeg({
        quality: 100,
      })
      .tile({
        size: 256,
      })
      .toFile(tmp_output_path)
  }
} catch (error) {
  console.log(`error: ${error}`)
}
